﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Guru.Web.UI;
using System.Collections.Specialized;

namespace Guru.Web.Controls.Telerik
{
    
    public class GuruGrid : RadGrid, IMultiLanguageLoader
    {
        public GuruGrid(): base()
        {
            this.Skin = GuruConfigurations.GuruSkinName;
            this.EnableEmbeddedSkins = false;
            this.AllowPaging = this.AllowSorting = true;
            this.AutoGenerateColumns = false;
            this.PagerStyle.AlwaysVisible = true;
            this.ClientSettings.EnableRowHoverStyle = false;
            this.PagerTemplatePath = "~/UserControl/GridPager.ascx";
        }

        public string PagerTemplatePath
        {
            set;
            get;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.LoadTextFromResource(((GuruPage)this.Page).CurrentLanguage);
            if (!string.IsNullOrEmpty(PagerTemplatePath))
                this.MasterTableView.PagerTemplate = Page.LoadTemplate(PagerTemplatePath);
        }       

        protected override bool OnNeedDataSource(GridNeedDataSourceEventArgs e)
        {
            bool isError = base.OnNeedDataSource(e);
            if (base.DataSource is DataTable)
            {
                DataTable dataSource = (DataTable)base.DataSource;
                base.PagerStyle.AlwaysVisible = base.AllowSorting = dataSource.Rows.Count != 0;
            }
            return isError;
        }

        public void SetText(NameValueCollection text)
        {
            foreach (GridColumn col in this.Columns)
            {
                if (!string.IsNullOrEmpty(col.UniqueName))
                {
                    col.HeaderText = text[string.Format("{0}.Columns.{1}", this.ID, col.UniqueName)];
                    col.HeaderTooltip = text[string.Format("{0}.Columns.{1}.HeaderTooltip", this.ID, col.UniqueName)];
                }
            }
        }
    }   
}

